package com.data.third;

import javassist.expr.NewArray;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.component.http.HttpConfig;
import com.component.http.HttpUtil;
import com.component.mode.ReturnMsg;


public class DataService {
	private static Logger logger = Logger.getLogger(DataService.class);
	public static JSONArray yesterdayRanks = null;

	/** 获得玩家基础信息与辅助信息 **/
	public static ReturnMsg getPlayeBaseInfo(long playerId) {
		JSONObject json = new JSONObject();
		json.put("action", "playerInfo");
		json.put("playerID", playerId);
		// 调用第三方接口进行登录
		ReturnMsg rmsg =null;
		try {
			rmsg = 	HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.no_encrypt_java, "hallServlet");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return rmsg;
	}

	/** 获得背包信息 **/
	public static ReturnMsg getPlayerBagInfo(long playerId) {
		JSONObject json = new JSONObject();
		json.put("action", "bagInfo");
		json.put("playerID", playerId);
		// 调用第三方接口进行登录
		ReturnMsg rmsg = new ReturnMsg();
		try {
			rmsg = HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.no_encrypt_java, "hallServlet");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return rmsg;
	}
	
	/** 检查token是否正确 **/
	public static ReturnMsg checkToken(long playerId, String token, String P, int platformId) {

		JSONObject json = new JSONObject();
		json.put("token", token);
		json.put("P", P);
		json.put("playerID", playerId);
		json.put("platformId", platformId);
		json.put("action", "analyseToken");
		// 调用第三方接口进行登录
		ReturnMsg rmsg = new ReturnMsg();
		try {
			rmsg = HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.encrypt_java, "loginServlet");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		json.clear();
		return rmsg;
	}

	//判断是否在哪个房间中
	public static ReturnMsg roomReConQuery(long playerId) {
		JSONObject json = new JSONObject();
		json.put("action", "roomReConQuery");
		json.put("playerID", playerId);
		// 调用第三方接口进行登录
		ReturnMsg rmsg = new ReturnMsg();
		try {
			rmsg = HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.no_encrypt_java, "hallServlet");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		json.clear();
		return rmsg;
	}
	//判断是否在哪个桌子中
	public static ReturnMsg deskReConQuery(long playerId, int gameId, int roomId) {
		JSONObject json = new JSONObject();
		json.put("action", "deskReConQuery");
		json.put("playerID", playerId);
		json.put("roomID", roomId);
		json.put("gameID", gameId);
		// 调用第三方接口进行登录
		ReturnMsg rmsg = new ReturnMsg();
		try {
			rmsg = HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.no_encrypt_java, "hallServlet");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		json.clear();
		return rmsg;
	}

	/** 进入游戏房间 **/
	public static boolean intoRoom(long playerId, int gameId, int roomId) {
		boolean flag = false;
		JSONObject json = new JSONObject();
		json.put("action", "intoRoom");
		json.put("playerID", playerId);
		json.put("gameID", gameId);
		json.put("roomID", roomId);
		// 调用第三方接口进行登录
		ReturnMsg rmsg = new ReturnMsg();
		try {
			rmsg = HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.encrypt_java, "hallServlet");
			logger.info(playerId + ",进入房间。");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			flag = false;
			return flag;
		}
		json.clear();
		if (rmsg.getState() == 1) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}
	
	/** 进入桌子 **/
	public static boolean intoDesk(long playerId, int gameId, int roomId, int deskId, int seatId) {
		boolean flag = false;
		JSONObject json = new JSONObject();
		json.put("action", "intoDesk");
		json.put("playerID", playerId);
		json.put("gameID", gameId);
		json.put("roomID", roomId);
		json.put("deskID", deskId);
		json.put("seatID", seatId);
		// 调用第三方接口进行登录
		ReturnMsg rmsg = new ReturnMsg();
		try {
			rmsg = HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.encrypt_java, "hallServlet");
			logger.info(playerId + ",进入桌子。");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			flag = false;
			return flag;
		}
		json.clear();
		if (rmsg.getState() == 1) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}

	/** 离开桌子 0-离开 1-换桌 **/
	public static boolean leaveDesk(long playerId, int gameId, int roomId, int deskId, int seatId, int type) {

		boolean flag = false;
		JSONObject json = new JSONObject();
		json.put("action", "leaveDesk");
		json.put("playerID", playerId);
		json.put("gameID", gameId);
		json.put("roomID", roomId);
		json.put("deskID", deskId);
		json.put("seatID", seatId);
		json.put("type", type);
		// 调用第三方接口进行登录
		ReturnMsg rmsg = new ReturnMsg();
		try {
			rmsg = HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.encrypt_java, "hallServlet");
			logger.info(playerId + ",离开桌子。");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			flag = false;
			return flag;
		}
		json.clear();
		if (rmsg.getState() == 1) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}
	/**
	 * 重置房间桌子进出时间
	 * @param playerId
	 * @param gameId
	 * @param roomId
	 * @return
	 */
	public static boolean resetTime(long playerId, int gameId, int roomId, int type){
		JSONObject json = new JSONObject();
		json.put("action", "resetTime");
		json.put("playerID", playerId);
		json.put("gameID", gameId);
		json.put("roomID", roomId);
		json.put("type", type);
		
		try {
			ReturnMsg rmsg = HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.encrypt_java, "hallServlet");
			if (rmsg.getState() == 1)
				return true;
			else
				return false;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}
	/** 离开游戏房间 **/
	public static boolean leaveRoom(long playerId, int gameId, int roomId) {
		boolean flag = false;
		JSONObject json = new JSONObject();
		json.put("action", "leaveRoom");
		json.put("playerID", playerId);
		json.put("gameID", gameId);
		json.put("roomID", roomId);
		// 调用第三方接口进行登录
		ReturnMsg rmsg = new ReturnMsg();
		try {
			rmsg = HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.encrypt_java, "hallServlet");
			logger.info(playerId + ",离开房间。");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			flag = false;
			return flag;
		}
		json.clear();
		if (rmsg.getState() == 1) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}
	
	/** 读取明天排行数据 **/
	/*public static String loadYesterdayRank(IoSession session) {
		if (yesterdayRanks == null) {
			net.sf.json.JSONObject jsonObject = null;
			jsonObject = Util.PlayerService.getYingQianRankForYesterday();
			if (!jsonObject.getBoolean("flag")) {
				session.write(SocketEnum.infoI + "@" + TishiManager.Tishi_YesterRankInfo_FAILL + "@&");
				return "";
			} else {
				yesterdayRanks = (net.sf.json.JSONArray) jsonObject.get("data");
				return yesterdayRanks.toString();
			}
		} else {
			return yesterdayRanks.toString();
		}
	}*/



	/**
	 * 游戏数据结算
	 * 
	 * detail --> 道具详情（propID:propNum;propID:propNum）
	 **/
	public static boolean updatePlayerPropInfo(Long playerId, int gameId, Integer changeSource, String detail) {
		boolean flag = false;
		JSONObject json = new JSONObject();
		json.put("action", "update");
		//json.put("beforeMoney", beforeMoney);
		json.put("playerID", playerId);
		json.put("gameID", gameId);
		json.put("detail", detail);
		json.put("changeSource", changeSource);
		// 调用第三方接口进行登录
		ReturnMsg rmsg = new ReturnMsg();
		try {
			rmsg = HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.encrypt_java, "hallServlet");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
		json.clear();
		if (rmsg.getState() == 1) {
			flag = true;
		}
		return flag;
	}
	
	/** 发送全服公告 **/
	public static boolean sendSms(String content) {
		boolean flag = false;
		JSONObject json = new JSONObject();
		json.put("showType", 0);
		json.put("level", 0);
		json.put("target", 0);
		json.put("action", "adBroadcast");
		json.put("content", content);
		// 调用第三方接口进行登录
		ReturnMsg rmsg = new ReturnMsg();
		try {
			rmsg = HttpUtil.sendData(json, HttpConfig.send_post, HttpConfig.encrypt_java, HttpConfig.third_sms_servlet);
			System.out.println("senSms:" + JSONObject.fromObject(rmsg).toString());
		} catch (Exception e) {
			e.printStackTrace();
			// MyPrint.print("checkToken fail");
			return false;
		}
		json.clear();
		if (rmsg.getState() == 1) {
			flag = true;
		}
		return flag;
	}
}
